# 6.8 Shared Object Extension

The addSharedObject method allows you to add a specific object as a shared object that can be used globally. Below is a code example:

public void configEngine(Engine me) {
   me.addSharedObject("RESOURCE_HOST", "http://res.jfinal.com");
   me.addSharedObject("StrKit", new com.jfinal.kit.StrKit());
}
1
2
3
4

In the code above, the second line adds a shared object named RESOURCE_HOST, while the third line adds another shared object named StrKit. Here's how to use them in a template:

<img src="#(RESOURCE_HOST)/img/girl.jpg" />
#if(StrKit.isBlank(title))
   ...
#end
1
2
3
4

In the first line of the example above, the output directive outputs the RESOURCE_HOST shared variable. For large web application systems, this approach conveniently organizes the servers where resource files are located. The second line calls the isBlank method of the shared object named StrKit. The usage is intuitive for developers.

Note: Since objects are shared globally, care must be taken to ensure thread safety. It is best to only share constants and stateless objects.

Last Updated: 9/22/2023, 4:50:57 AM